package leetcode.code524;

import java.util.Collections;
import java.util.List;

public class Solution1 extends Solution524 {

	@Override
	public String findLongestWord(String s, List<String> dictionary) {
		Collections.sort(dictionary, (o1, o2) -> {
			if (o1.length() != o2.length()) {
				return o2.length() - o1.length();
			} else {
				return o1.compareTo(o2);
			}
		});
		for (String str : dictionary) {
			if (substring(str, s)) {
				return str;
			}
		}
		return "";
	}

	private boolean substring(String str, String s) {
		int p1 = 0;
		int p2 = 0;
		while (p1 < str.length() && p2 < s.length()) {
			if (str.charAt(p1) == s.charAt(p2)) {
				p1++;
			}
			p2++;
		}
		return p1 == str.length() ? true : false;
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
//		String a = "abc";
//		String b = "acd";
//		System.out.println(a.compareTo(b));
	}

}
